/**
 * 
 */
package com.cater.talents.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Company;
import com.cater.sys.service.impl.CompanyServiceImpl;
import com.cater.talents.entity.WorkExperience;
import com.cater.talents.service.ExpProjectServiceImpl;
import com.cater.talents.service.WorkExperienceServiceImpl;

/**
 * @Description: TODO
 * @author: yong
 * @time:2018年3月29日 下午11:37:57
 * @工作经历控制层
 */
@Controller
@RequestMapping("/work")
public class WorkExperienceController {

	@Autowired
	private WorkExperienceServiceImpl service;
	@Autowired
	private CompanyServiceImpl companyService;// 公司
	@Autowired
	private ExpProjectServiceImpl expProjectService;// 项目经验

	/**
	 * @工作经历列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/resume/list_work";
	}

	/**
	 * @工作经历数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询工作经历分页数据")
	public PageResult<WorkExperience> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<WorkExperience> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("workExperience:add")
	public String addView(Model model, String talents_id, CommonQuery query) {
		model.addAttribute("talents_id", talents_id);
		return "talents/workExperience/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("workExperience:add")
	@ResponseBody
	@SystemControllerLog(description = "添加工作经历")
	public JSONObject add(Integer num, HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		try {
			for (int i = 1; i < num + 1; i++) {
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				// String companyId = request.getParameter("companyId" + i);
				String temp_companyName = request.getParameter("companyName" + i);
				String companyName = null;
				if (StringUtils.isNotEmpty(temp_companyName)) {
					companyName = temp_companyName.replaceAll("'", "");
				}
				String position = request.getParameter("position" + i);
				String depName = request.getParameter("depName" + i);
				String report = request.getParameter("report" + i);
				String subordinateNum = request.getParameter("subordinateNum" + i);
				String responsibility = request.getParameter("responsibility" + i);
				String performance = request.getParameter("performance" + i);
				String project = request.getParameter("project" + i);
				String job = request.getParameter("job" + i);
				String workplace = request.getParameter("workplace" + i);
				String description = request.getParameter("description" + i);
				if (StringUtils.isNotEmpty(companyName)) {
					WorkExperience wExperience = new WorkExperience();
					Company company = new Company();
					query.setName(companyName);
					company = companyService.getByName(query);
					if (company != null) {
						company.setId(company.getId());
					} else {
						company = new Company();
						company.setName(companyName);
						String company_id = companyService.saves(company);
						company.setId(company_id);
					}
					wExperience.setCompany(company);
					wExperience.setCompanyName(companyName);
					wExperience.setTalentsId(talentid);
					wExperience.setStartDate(startDate);
					if (StringUtils.isNotEmpty(endDate)) {
						wExperience.setEndDate(endDate);
					} else {
						wExperience.setEndDate("至今");
					}
					wExperience.setPosition(position);
					wExperience.setDepId(depName);
					wExperience.setReport(report);
					wExperience.setSubordinateNum(Integer.valueOf(subordinateNum));
					wExperience.setResponsibility(responsibility);
					wExperience.setPerformance(performance);
					wExperience.setProject(project);
					/* wExperience.setProjectNames(projectNames); */
					wExperience.setJob(job);
					wExperience.setWorkplace(workplace);
					wExperience.setDescription(description);
					service.saves(wExperience);
					json.put("code", 200);
					json.put("message", "添加成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "添加失败");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("workExperience:edit")
	public String editView(Model model, CommonQuery query) {
		model.addAttribute("talentsId", query.getTalentsId());
		List<WorkExperience> t = service.getListByTalentsId(query);
		model.addAttribute("worknum", t.size());
		model.addAttribute("workExperiences", t);
		return "talents/workExperience/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("workExperience:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改工作经历")
	public JSONObject update(Integer num, HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		String talentid = request.getParameter("talentsId");
		try {
			query.setTalentsId(talentid);
			List<WorkExperience> workExperiences = service.getListByTalentsId(query);
			for (int i = 1; i < num + 1; i++) {
				String id = request.getParameter("id" + i);
				String startDate = request.getParameter("startDate" + i);
				String endDate = request.getParameter("endDate" + i);
				// String companyId = request.getParameter("companyId" + i);
				String temp_companyName = request.getParameter("companyName" + i);
				String companyName = null;
				if (StringUtils.isNotEmpty(temp_companyName)) {
					companyName = temp_companyName.replaceAll("'", "");
				}
				String position = request.getParameter("position" + i);
				String depName = request.getParameter("depName" + i);
				String report = request.getParameter("report" + i);
				String subordinateNum = request.getParameter("subordinateNum" + i);
				String responsibility = request.getParameter("responsibility" + i);
				String performance = request.getParameter("performance" + i);
				String project = request.getParameter("project" + i);
				/* String projectNames = request.getParameter("projectNames" + i); */
				String job = request.getParameter("job" + i);
				String workplace = request.getParameter("workplace" + i);
				String description = request.getParameter("description" + i);

				for (WorkExperience workExperience : workExperiences) {
					if (workExperience.getId().equals(id)) {
						Company company = new Company();
						query.setName(companyName);
						company = companyService.getByName(query);
						if (company != null) {
							company.setId(company.getId());
						} else {
							company = new Company();
							company.setName(companyName);
							company.setProfile(description);
							String company_id = companyService.saves(company);
							company.setId(company_id);
						}
						workExperience.setCompany(company);
						workExperience.setTalentsId(talentid);
						workExperience.setStartDate(startDate);
						if (StringUtils.isNotEmpty(endDate)) {
							workExperience.setEndDate(endDate);
						} else {
							workExperience.setEndDate("至今");
						}
						workExperience.setPosition(position);
						workExperience.setDepId(depName);
						workExperience.setReport(report);
						workExperience.setSubordinateNum(Integer.valueOf(subordinateNum));
						workExperience.setResponsibility(responsibility);
						workExperience.setPerformance(performance);
						workExperience.setProject(project);
						/* workExperience.setProjectNames(projectNames); */
						workExperience.setJob(job);
						workExperience.setWorkplace(workplace);
						workExperience.setDescription(description);
						service.update(workExperience);
					}
				}
				if (StringUtils.isEmpty(id)) {
					if (StringUtils.isNotEmpty(companyName)) {
						WorkExperience wExperience = new WorkExperience();
						Company company = new Company();
						query.setName(companyName);
						company = companyService.getByName(query);
						if (company != null) {
							company.setId(company.getId());
						} else {
							company = new Company();
							company.setName(companyName);
							String company_id = companyService.saves(company);
							company.setId(company_id);
						}
						wExperience.setCompany(company);
						wExperience.setCompanyName(companyName);
						wExperience.setTalentsId(talentid);
						wExperience.setStartDate(startDate);
						if (StringUtils.isNotEmpty(endDate)) {
							wExperience.setEndDate(endDate);
						} else {
							wExperience.setEndDate("至今");
						}
						wExperience.setPosition(position);
						wExperience.setDepId(depName);
						wExperience.setReport(report);
						wExperience.setSubordinateNum(Integer.valueOf(subordinateNum));
						wExperience.setResponsibility(responsibility);
						wExperience.setPerformance(performance);
						wExperience.setProject(project);
						/* wExperience.setProjectNames(projectNames); */
						wExperience.setJob(job);
						wExperience.setWorkplace(workplace);
						wExperience.setDescription(description);
						service.saves(wExperience);
						/*
						 * String workId = service.saves(wExperience);
						 * if(StringUtils.isNotEmpty(workId)) { String[] expProjectId =
						 * project.split(","); for (String expId : expProjectId) {
						 * if(StringUtils.isNotEmpty(expId)) { ExpProject expProject =
						 * expProjectService.getById(expId); if(expProject!=null) {
						 * expProject.setWorkExpId(workId); expProjectService.update(expProject); } } }
						 * }
						 */
					}
				}
			}
			json.put("code", 200);
			json.put("message", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "操作失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("workExperience:delete")
	@SystemControllerLog(description = "删除工作经历")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping(value = "editSingleView")
	public String editSingleView(String id, Model model) {
		WorkExperience workExperience = service.getById(id);
		model.addAttribute("workExp", workExperience);
		return "talents/workExperience/edit_single";

	}

	@RequestMapping(value = "editSingle")
	@ResponseBody
	@SystemControllerLog(description = "修改单个工作经历")
	public JSONObject updateSingle(WorkExperience workExperience, HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String companyName = workExperience.getCompanyName();
			String endDate = workExperience.getEndDate();
			if (StringUtils.isNotEmpty(companyName)) {
				Company company = new Company();
				query.setName(companyName);
				company = companyService.getByName(query);
				if (company != null) {
					company.setId(company.getId());
				} else {
					company = new Company();
					company.setName(companyName);
					String company_id = companyService.saves(company);
					company.setId(company_id);
				}
				workExperience.setCompany(company);
				if (StringUtils.isNotEmpty(endDate)) {
					workExperience.setEndDate(endDate);
				} else {
					workExperience.setEndDate("至今");
				}
				service.update(workExperience);
				json.put("code", 200);
				json.put("message", "修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "修改失败");
		}
		return json;

	}

	@ResponseBody
	@RequestMapping("/getJsonBy")
	public JSONObject getJsonBy(HttpServletRequest request, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			List<WorkExperience> list = service.getListBy(query);
			if (list != null && list.size() > 0) {
				json.put("datas", list);
				json.put("code", 200);
				json.put("msg", "数据获取成功");
			} else {
				json.put("code", 400);
				json.put("msg", "暂无数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("msg", "数据获取异常");
		}
		return json;
	}
}
